home *** CD-ROM | disk | FTP | other *** search
Java Source | 2003-12-19 | 2.9 KB | 97 lines |
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.util.*;
-
- public class BloccoNotes implements WindowListener, ActionListener{
-
- //Istanze della classe
- private Frame finestra;
- private TextArea areaTesto;
-
- //Costruttore della classe
-
- public BloccoNotes(Frame f, TextArea t) {
- finestra = f;
- areaTesto = t;
- }
-
- //Gestione della grafica
-
- // Metodi non implementati dell'interfaccia WindowListener
- public void windowActivated(WindowEvent evt) {}
- public void windowDeactivated(WindowEvent evt) {}
- public void windowDeiconified(WindowEvent evt) {}
- public void windowIconified(WindowEvent evt) {}
- public void windowOpened(WindowEvent evt) {}
-
- // Metodi implementati dell'interfaccia WindowListener
- public void windowClosed(WindowEvent evt) {
- System.exit(0);
- }
-
- public void windowClosing(WindowEvent evt) {
- finestra.dispose();
- }
-
- // Metodo acoltatore ha il computo di ascoltare i comandi
- public void actionPerformed(ActionEvent evt) {
- String comando = evt.getActionCommand();
- //gestione dei vari eventi
- if (comando.equals("Esci")) {
- finestra.dispose();
- } else if (comando.equals("Apri")) {
- areaTesto.setText("");
- FileDialog d = new FileDialog(finestra, "Apri documento", FileDialog.LOAD);
- d.setVisible(true);
- if (d.getFile() != null) {
- try {
- String line;
- BufferedReader in = new BufferedReader(new FileReader(d.getDirectory() + File.separator + d.getFile()));
- while ((line = in.readLine()) != null)
- areaTesto.append(line + "\n");
- } catch (Exception e) {
- System.out.println("Errore: " + e);
- }
- }
- } else if (comando.equals("Salva con nome...")) {
- FileDialog d = new FileDialog(finestra, "Salva documento con nome", FileDialog.SAVE);
- d.setVisible(true);
- PrintWriter fout;
- String Stringa=areaTesto.getText();
- try{
- fout=new PrintWriter(new FileWriter(d.getDirectory()+File.separator+d.getFile()));
- StringTokenizer st=new StringTokenizer(Stringa,"\n"); //E' importante per gestire la pressione dell'invio
- while (st.hasMoreTokens())
- fout.println(st.nextToken());
- fout.close();
- }catch(Exception e){}
- }
- }
-
- /** Metodo Main */
- public static void main(String[] args) {
- Frame f = new Frame("My blocco note ");
- TextArea t = new TextArea();
- BloccoNotes n = new BloccoNotes(f, t);
- MenuBar mb = new MenuBar();
-
- // Preparazione men∙ file il menu File
- Menu menu = new Menu("File");
- mb.add(menu);
- menu.add("Apri");
- menu.add("Salva con nome...");
- menu.addSeparator();
- menu.add("Esci");
-
- menu.addActionListener(n);
-
- // Prepara la finestra
- f.addWindowListener(n);
- f.setMenuBar(mb);
- f.add(t);
- f.setSize(400, 500);
- f.setVisible(true);
- }
- } // BloccoNotes
-